package com.fadadianqi.starter;

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Logger;

/**
 * @author aroot
 */
public class ServerClassLoader extends URLClassLoader {

    private static Logger logger = Logger.getLogger("ServerStarter");

    public ServerClassLoader(ClassLoader parent, File confDir, File libDir)
            throws MalformedURLException {
        super(new URL[]{confDir.toURI().toURL(), libDir.toURI().toURL()},
                parent);

        File[] jars = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean accept = false;
                String smallName = name.toLowerCase();
                if (smallName.endsWith(".jar")) {
                    accept = true;
                } else if (smallName.endsWith(".zip")) {
                    accept = true;
                }
                return accept;
            }
        });

        if (jars == null) {
            return;
        }

        for (int i = 0; i < jars.length; i++) {
            //logger.info("load..." + jars[i].getAbsolutePath());
            if (jars[i].isFile()) {
                addURL(jars[i].toURI().toURL());
            }
        }
    }

}
